<template>
  <el-form label-width="100px" ref="formRef" :model="dataForm" :rules="dataRule">
    <el-card class="box-card" shadow="never">
      <div class="text item">
        <el-form-item class="mavon" prop="content" :label="i18n('place.content')">
          <editor ref="editorRef" :content="dataForm.content"></editor>
        </el-form-item>
      </div>
    </el-card>

    <el-card class="box-card mt10" shadow="never">
      <div class="text item">
        <el-form-item>
          <el-button
            type="primary"
            @click="dataFormSubmit(formRef)">
            {{ i18n('common.confirm') }}
          </el-button>
          <el-button @click="resetForm(formRef)">
            {{ i18n('common.reset') }}
          </el-button>
        </el-form-item>
      </div>
    </el-card>
  </el-form>
</template>

<script setup name="place-detail-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import Editor from '@/components/form/editorjs/editorjs'

  const { router, query, i18n, isAuth } = useBase()

  const props = defineProps({
    model: {
      type: String,
      default: null,
    }
  });

  const editorRef = ref()

  const dataForm = reactive({
    id: 0,
    content: '',
  })

  const dataRule = reactive({})

  const {
    formRef,
    resetForm
  } = useForm(props.model, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${props.model}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${props.model}/detail/data`),
              method: 'get',
              params: $http.adornParams({
                'place_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data.data && data.status === 200) {
                dataForm.content = data.data.content
              }
            })
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${props.model}/second_submit`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            content: editorRef.value.data,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  onMounted(() => {
    init(formRef)
  })
</script>
